package com.tuzhi.sys.service.impl;

import com.tuzhi.base.jpa.healper.QueryHelper;
import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.base.service.impl.AbsBaseService;
import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.sys.jpa.entity.SysRoleEntity;
import com.tuzhi.sys.jpa.entity.SysUserEntity;
import com.tuzhi.sys.jpa.entity.SysUserRoleEntity;
import com.tuzhi.sys.jpa.repository.SysRoleRepo;
import com.tuzhi.sys.jpa.repository.SysUserRepo;
import com.tuzhi.sys.service.SysRoleService;
import com.tuzhi.sys.service.SysUserRoleService;
import com.tuzhi.util.UserContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl extends AbsBaseService<SysRoleEntity, Long> implements SysRoleService {

    private static final Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    SysRoleRepo repo;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysUserRepo userRepo;



    @Override
    protected BaseRepo getThisRepo() {
        return repo;
    }

    

    @Override
    public List<SysRoleEntity> listRoleEfficacious() {
        LoginUserBO userInfo = UserContext.getUserInfo(true);
        QueryHelper.Builder<SysRoleEntity> builder = new QueryHelper.Builder<>();
        builder.eq(SysRoleEntity::getSysStatus, 0).eq(SysRoleEntity::getSysStatus, 0);
        //只能查出小于等于本用户最高优先级角色
        Integer maxPriority = (Integer)getMaxPriorityRole(userInfo.getUserId()).get("priority");
        if (maxPriority == 10){
            List roleNames = repo.getRoleNamesByUserId(userInfo.getUserId());
            builder.in(SysRoleEntity::getName, roleNames);
        }else {
            builder.gt(SysRoleEntity::getPriority, maxPriority - 1);
        }
        return findAllByWhereConditionBO(builder.build().getWhereConditionBO());
    }

    @Override
    public List<SysRoleEntity> listUserRole(Long userId) {
        List<Long> roleId = sysUserRoleService.listRoleId4UserId(userId);
        List<SysRoleEntity> roles = new ArrayList<>();
        if (roleId != null && !roleId.isEmpty()) {
            roleId.forEach(rid -> {
                SysRoleEntity entity = get(rid);
                if (entity != null) {
                    roles.add(get(rid));
                }
            });
        }
        return roles;
    }

    @Override
    public List<SysRoleEntity> getRoles(List<String> roleNames) {
        QueryHelper.Builder<SysRoleEntity> builder = new QueryHelper.Builder<>();
        builder.in(SysRoleEntity::getName, roleNames);
        return repo.findAll(builder.build().getWhereConditionBO());
    }

    @Override
    public Map getMaxPriorityRole(Long userId){
        return repo.getMaxPriorityRole(userId);
    }



    //查询角色所有的用户
    @Override
    public List<SysUserEntity> getUserByRoleId(Long id) {
        List<SysUserRoleEntity> sysUserRoleList=sysUserRoleService.listUserIdByRoleId(id);
        QueryHelper.Builder<SysUserEntity> builder = new QueryHelper.Builder<>();
        List<Long> listId=new ArrayList<>();
        sysUserRoleList.forEach(SysUserRole->{
            if ( SysUserRole.getUserId()!=null){
                listId.add(SysUserRole.getUserId());
            }

        });
        builder.in(SysUserEntity::getId,listId);
        List<SysUserEntity> sysUserList = userRepo.findAll(builder.build().getWhereConditionBO());
        return sysUserList;
    }
}
